package com.subang.function.controller;

import cn.hutool.json.JSONObject;
import com.subang.common.utils.WeChatUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/weChat")
public class msgSecCheck {

    //微信小程序appId
    @Value("${wechat.appId}")
    private String appId;

    //微信小程序密钥
    @Value("${wechat.appSecret}")
    private String appSecret;

    /**
     * 检测文本内容是否含有违法违规内容
     *
     * @param request     要检测的文本内容
     * @return 检测结果的JSON对象
     */
    @PostMapping("/msgSecCheck")
    public JSONObject msgSecCheck(@RequestBody MsgSecCheckRequest request) {
        String content = request.getContent();
        String accessToken = WeChatUtil.getAccessToken(appId, appSecret).getStr("access_token");
        JSONObject result = WeChatUtil.msgSecCheck(accessToken, content);
        return result;
    }


    // 定义为静态内部类
    public static class MsgSecCheckRequest {
        private String content;

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }
    }

    /**
     * 获取access_token
     *
     * @return access_token信息
     */
    @GetMapping("/getAccessToken")
    public JSONObject getAccessToken() {
        JSONObject result = WeChatUtil.getAccessToken(appId, appSecret);
        return result;
    }
}